#include "user_service.h"

UserService::UserService(std::shared_ptr<MysqlConnection>sql_conn) :sql_conn_(sql_conn) { 

}

UserService::~UserService() {
	sql_conn_.reset();
}

bool UserService::exit(const std::string& mobile) {
	char sql_content[1024] = {0};	
	sprintf(sql_content, "select * from userinfo where mobile = %s", mobile.c_str());
						  //select * from userinfo where mobile = 17777777777

	SqlRecordSet record_set;
	if (!sql_conn_->Execute(sql_content, record_set)) {
		printf("user service excute failed: \n");
		return false;
	}
	printf("user service search row: %d\n", record_set.GetRowCount());
	return (record_set.GetRowCount() != 0);
	//return sql_conn_->Execute(sql_content);
}

bool UserService::insert(const std::string& mobile) {
	char sql_content[1024] = { 0 };
	sprintf(sql_content, "insert into userinfo (mobile) values ('%s')", mobile.c_str());

	return sql_conn_->Execute(sql_content);
}

i32 UserService::recharge(const std::string& mobile, int amount)
{
	char sql_content[1024] = { 0 };
	sprintf(sql_content, "select money from userinfo where mobile = %s", mobile.c_str());

	SqlRecordSet record_set;
	if (!sql_conn_->Execute(sql_content, record_set)) {
		return -1;
	}

	MYSQL_ROW row = record_set.FetchRow();
	printf("userservice:: data =  %s\n",*row); // 获取结果集的第一行第一列数据

	int money = atoi(*row) + amount; // 将字符串转换为整数

	memset(sql_content, 0, sizeof(sql_content));
	sprintf(sql_content, "update userinfo set money = %d where mobile = %s", money, mobile.c_str());
	sql_conn_->Execute(sql_content);

	return money;
}
